package cz.cvut.kadledav.paa.weight3sat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class ScatterGraph extends JFrame {

    public ScatterGraph(XYDataset dataSet, String params) {

        JFreeChart jfreechart = ChartFactory.createScatterPlot( "Simulované ochlazování\n" +params, "počet iterací", "optimalizační kriterium", dataSet,PlotOrientation.VERTICAL, true, true, false);
        XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
        xyPlot.setDomainCrosshairVisible(true);
        xyPlot.setRangeCrosshairVisible(true);
        xyPlot.setFixedLegendItems(new LegendItemCollection());    //hide legend
        XYItemRenderer renderer = xyPlot.getRenderer();
        double size = 1.0;
        double delta = size / 2.0;
        Shape shape2 = new Ellipse2D.Double(-delta, -delta, size, size);
        renderer.setSeriesShape(0, shape2);
        renderer.setSeriesPaint(0, Color.BLACK);
        xyPlot.setBackgroundPaint(Color.white);

        ChartPanel chart = new ChartPanel(jfreechart);
        chart.setBorder(BorderFactory.createLineBorder(Color.white,15));
        this.add(chart, BorderLayout.CENTER);

        setPreferredSize(new Dimension(900,600));
        JPanel control = new JPanel();
    }

    public static void main(String args[]) {
        List<Double> xData = new ArrayList<>();
        List<Double> yData = new ArrayList<>();
        for (int i = 0; i < 5000; i = i + 20) {
            xData.add((double) i);
            yData.add((double) i / 50);
        }
        showScatterGraph(xData, yData,"asda");
    }



    public static void showScatterGraph(List<Double> x, List<Double> y, final String params) {
        showScatterGraph(x.iterator(),y.iterator(),params);
    }


    public static void showScatterGraph(Iterator<Double> y, final String params,int increment) {
        if(increment <= 0 ){
            increment = 1;
        }
        double count = increment;
        final XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        final XYSeries series = new XYSeries("Data");
        while (y.hasNext()){
            series.add(count,(double)y.next());
            count += increment;
        }
        xySeriesCollection.addSeries(series);
        show(params, xySeriesCollection);
    }

    public static void showScatterGraph(Iterator<Double> x, Iterator<Double> y, final String params) {
        final XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
        final XYSeries series = new XYSeries("Data");
        while (x.hasNext() && y.hasNext()){
            series.add(x.next(), y.next());
        }
        xySeriesCollection.addSeries(series);
        show(params, xySeriesCollection);
    }

    private static void show(final String params, final XYSeriesCollection xySeriesCollection) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                ScatterGraph demo = new ScatterGraph(xySeriesCollection, params);
                demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                demo.pack();
                demo.setLocationRelativeTo(null);
                demo.setVisible(true);
            }
        });
    }
}